package com.kycxika.app.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.kycxika.app.R;
import com.kycxika.app.activity.base.BaseActivity;
import com.kycxika.app.bean.AuthorizedBean;
import com.kycxika.app.bean.IsBlackBean;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.dbutils.inter.HttpQuestResultListener;
import com.kycxika.app.utils.APPUtil;
import com.kycxika.app.utils.HttpApiConst;
import com.kycxika.app.utils.PreferenceHelper;
import com.kycxika.app.view.LoadingView;
import com.kycxika.app.view.ShapeLoadingDialog;
import com.tbruyelle.rxpermissions.RxPermissions;
import rx.functions.Action1;

import java.util.List;


public class WelcomeActivity extends BaseActivity implements HttpQuestResultListener {

    public KycXkIApi httpApi;
   public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    startActivity(new Intent(WelcomeActivity.this,MainTwoActivity.class));
                    WelcomeActivity.this.finish();
                    break;
            }
        }
    };

    private ShapeLoadingDialog shapeLoadingDialog;
    private LoadingView loadView;

    @Override
    protected void initView() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if (granted) {
                        } else {
                        }
                    }
                });



        httpApi = KycXkIApi.getInstance(this);
        httpApi.setHttpQuestResultListener(this);

       /* loadView = (ShapeLoadingView) findViewById(R.id.loadView);
        loadView.setVisibility(View.VISIBLE);*/
        /*shapeLoadingDialog = new ShapeLoadingDialog.Builder(this)
                .loadText("加载中...")
                .build();*/

        loadView = (LoadingView) findViewById(R.id.loadView);
       /* loadView.setDelay(500);
        loadView.setLoadingText("加载中...");
        loadView.setVisibility(View.VISIBLE);
        //loadView.postDelayed(upRun,1000);
        loadView.upThrow();*/

    }

    /*Runnable upRun = new Runnable() {
        @Override
        public void run() {
            loadView.upThrow();
            loadView.postDelayed(fellRun,1000);
        }
    };

    Runnable fellRun = new Runnable() {
        @Override
        public void run() {
            loadView.freeFall();
            loadView.postDelayed(upRun,1000);
        }
    };*/

    @Override
    protected void onStart() {
        super.onStart();
        //shapeLoadingDialog.show();
        loadView.setVisibility(View.VISIBLE,800);
    }

    @Override
    protected void initData() {
        final String token = PreferenceHelper.getToken(this);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!TextUtils.isEmpty(token)) {
                    Log.i("TAG","token ---- " +token);
                    startActivity(new Intent(WelcomeActivity.this,MainTwoActivity.class));
                    WelcomeActivity.this.finish();
                }else {
                    String random = APPUtil.createRandom(false, 5);
                    PreferenceHelper.saveRandom(WelcomeActivity.this,random);
                    Log.i("TAG", "random ------ " + random);
                    String md5Str = APPUtil.md5(HttpApiConst.appSecret + HttpApiConst.appkey + random);
                    Log.i("TAG", "md5Str ------ " + md5Str);
                    httpApi.getAuthorized(random, md5Str);
                }
            }
        },4000);
       // httpApi.getVIPVistorList(0);
    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    public void onSuccess(Object object) {
        if(object instanceof AuthorizedBean) {
            AuthorizedBean authorizedBean = (AuthorizedBean) object;
            AuthorizedBean.DataBean dataBean = authorizedBean.getData();
            if(dataBean != null) {
                PreferenceHelper.saveToken(this, dataBean.getToken());
                Log.i("TAG","token  -----  " + dataBean.getToken());
                Message msg = Message.obtain();
                msg.what = 100;
                handler.sendMessage(msg);
            }
        }

        if(object instanceof IsBlackBean) {
            IsBlackBean isBlackBean = (IsBlackBean) object;
            List<IsBlackBean.DataBean.ItemsBean> itemsBeen = isBlackBean.getData().getItems();
            if(itemsBeen!= null && itemsBeen.size() > 0) {
                Log.i("TAG","itemsBeen ----- " +itemsBeen.get(0).getAddress());
            }else {
                Log.i("TAG","itemsBeen ----- 无数据");
            }
        }
    }

    @Override
    public void onQuestErroCode(Object object) {
        if(object instanceof AuthorizedBean) {
            AuthorizedBean authorizedBean = (AuthorizedBean) object;
            Log.i("TAG","code ---- " + authorizedBean.getCode());
            Toast.makeText(this, authorizedBean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onError(Object object) {
        if(object instanceof AuthorizedBean) {
            AuthorizedBean authorizedBean = (AuthorizedBean) object;
           Log.i("TAG","error ---- " + authorizedBean.getCode());
        }

        if(object instanceof IsBlackBean) {
            IsBlackBean isBlackBean = (IsBlackBean) object;
            Log.i("TAG","error ---- " + isBlackBean.getResponseCode());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loadView.setVisibility(View.GONE,0);
    }
}
